package de.openlaboratory.xmleditor.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.xml.sax.SAXException;

import de.openlaboratory.util.IsolatedStorage;
import de.openlaboratory.xmleditor.xml.EntityConstructor;

/**
 * Servlet implementation class EditorServlet
 */
@WebServlet(
		description = "Main XML file editor servlet",
		urlPatterns = { "/edit" },
		initParams = { @WebInitParam(name = "xmlfile", value = "none") }
)

public class EditorServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	private static final String EDIT_PAGE ="WEB-INF/jsp/editXmlFile.jsp";
	private static final String REPOSITORY ="res/xml";
	
	private static final String XML_FILE = "D:\\DEV\\openlaboratory\\Open Laboratory XML Editor\\web\\res\\xml\\jobs.xml";
	
	/**
	 * Default constructor.
	 */
	public EditorServlet()
	{
		// TODO Auto-generated constructor stub
	}

	private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException, TransformerException, ParserConfigurationException, SAXException
	{
	    IsolatedStorage xmlFile = getXmlFile(request);
	    
	    String n = request.getParameter("sName");
	    String v = request.getParameter("sValue");

	    request.setAttribute("xmlFile", xmlFile.getFileName());
	    
	    if(n != null)
	    {
	    	EntityConstructor.getNode(xmlFile.getLocalFile().toFile().getPath(), n, v);
		    
	    	String m;
	    	m = "Entry added: " + n;
	    	if (v != null)
	    	{
	    		m = m + " [" + v + "]";
	    	}
	    	
	    	request.setAttribute("message", m);
	    }
	    
		Cache xyz = CacheManager.getInstance().getCache("xyz");
		//xyz.put(new Element("myKey", "1324987"));
		Element e = xyz.get("myKey");
		String valoo = (String) e.getObjectValue();
		request.setAttribute("cachedValoo", valoo);
		
	    RequestDispatcher view = request.getRequestDispatcher(EDIT_PAGE);
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			doAction(request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		try
		{
			doAction(request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private IsolatedStorage getXmlFile(HttpServletRequest request)
	{
		String name = request.getParameter("xml_file");
		
		if ((name == null) || (name.length() == 0))
		{
			name = "jobs.xml";
		}
		
		ServletContext servletContext = getServletContext();
		String root = servletContext.getRealPath(File.separator);
				
		return new IsolatedStorage(root, REPOSITORY, name);
	}
}
